﻿using HalconDotNet;
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;

namespace Wpf.Halcon.Demo
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        HImage hImage = new HImage();
        HTuple hTuple = new HTuple();
        HTuple modelId = new HTuple();

        List<HDrawingObjectInfo> DrawList = new List<HDrawingObjectInfo>();

        /// <summary>
        /// 读取图片
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //读取图片
            hImage.ReadImage(@"E:\My_Code\Demo\wpf.-prism\src\xxx.jpeg");
            //将图片 显示在 窗口上
            HSmartName.HalconWindow.DispObj(hImage);
        }

        /// <summary>
        /// 定位结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        /// <summary>
        /// 生成圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            string color = "red";
            var circle = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.CIRCLE, 100, 200, 50);
            HSmartName.HalconWindow.AttachDrawingObjectToWindow(circle);

            HDrawingObjectInfo info = new HDrawingObjectInfo
            {
                Type = "circle",
                Color = color,
                HDrawingObject = circle,
                HTuples = circle.GetHTuples(),
            };
            DrawList.Add(info);

            //拖拽
            circle.OnDrag(OnDragDrawingObejct);
            //调整大小
            circle.OnResize(OnResizeDrawingObejct);
            //选中
            circle.OnSelect(OnSelectDrawingObejct);
        }

        /// <summary>
        /// 生成矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            string color = "bule";
            var rect = HDrawingObject.CreateDrawingObject(HDrawingObject.HDrawingObjectType.RECTANGLE1, 100, 200, 300, 300);
            HSmartName.HalconWindow.AttachDrawingObjectToWindow(rect);

            HDrawingObjectInfo info = new HDrawingObjectInfo
            {
                Type = "rectangle1",
                Color = color,
                HDrawingObject = rect,
                HTuples = rect.GetHTuples(),
            };
            DrawList.Add(info);

            //拖拽
            rect.OnDrag(OnDragDrawingObejct);
            //调整大小
            rect.OnResize(OnResizeDrawingObejct);
            //选中
            rect.OnSelect(OnSelectDrawingObejct);
        }

        /// <summary>
        /// 创建匹配模版
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            var drawInfo = DrawList.FirstOrDefault(r => r.Type == "rectangle1");

            if (drawInfo != null)
            {
                // 根据绘制对象的结构性数据生成对应的区域
                var rectObj = drawInfo.GenHObject();

                //获取图片对应区域中的图像（局部图像）
                HObject imageReduced;
                HObject cropImage;
                HOperatorSet.ReduceDomain(hImage, rectObj, out imageReduced);
                HOperatorSet.CropDomain(imageReduced, out cropImage);

                //根据图像生成模版
                HSmartName.HalconWindow.DispObj(cropImage);

                //生成模版
                HOperatorSet.CreateShapeModel(cropImage, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(),
                    "auto", "auto", "use_polarity", "auto", "auto", out modelId);

                HOperatorSet.WriteShapeModel(modelId, @"E:\My_Code\Demo\wpf.-prism\src\crop.shm");

            }

        }

        #region 回调事件

        /// <summary>
        /// 选中
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        private void OnSelectDrawingObejct(HDrawingObject drawid, HWindow window, string type)
        {
            RefreshDrawingObjectParam(drawid);
        }

        /// <summary>
        /// 调整大小
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        private void OnResizeDrawingObejct(HDrawingObject drawid, HWindow window, string type)
        {
            RefreshDrawingObjectParam(drawid);
        }

        /// <summary>
        /// 拖拽
        /// </summary>
        /// <param name="drawid"></param>
        /// <param name="window"></param>
        /// <param name="type"></param>
        private void OnDragDrawingObejct(HDrawingObject drawid, HWindow window, string type)
        {
            RefreshDrawingObjectParam(drawid);
        }

        #endregion


        /// <summary>
        /// 刷新画板对象参数
        /// </summary>
        /// <param name="drawingObject"></param>
        public void RefreshDrawingObjectParam(HDrawingObject drawingObject)
        {
            var drawInfo = DrawList.Where(r => r.HDrawingObject.ID.Equals(drawingObject.ID)).FirstOrDefault();
            if (drawInfo != null)
            {
                drawInfo.HTuples = drawingObject.GetHTuples();
            }
        }

        /// <summary>
        /// 绘制矩形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void Button_Click_5(object sender, RoutedEventArgs e)
        {
            HTuple row1 = new HTuple();
            HTuple column1 = new HTuple();
            HTuple row2 = new HTuple();
            HTuple column2 = new HTuple();

            await Task.Run(() =>
            {
                HOperatorSet.SetColor(HSmartName.HalconWindow, "blue");
                HOperatorSet.DrawRectangle1(HSmartName.HalconWindow, out row1, out column1, out row2, out column2);

                HObject rectObj;
                HOperatorSet.GenRectangle1(out rectObj, row1, column1, row2, column2);

                HObject imageReduced;
                HObject cropImage;
                HOperatorSet.ReduceDomain(hImage, rectObj, out imageReduced);
                HOperatorSet.CropDomain(imageReduced, out cropImage);

                //根据图像生成模版
                HSmartName.HalconWindow.DispObj(cropImage);

                //生成模版
                HOperatorSet.CreateShapeModel(cropImage, "auto", (new HTuple(0)).TupleRad(), (new HTuple(360)).TupleRad(),
                    "auto", "auto", "use_polarity", "auto", "auto", out modelId);

                HOperatorSet.WriteShapeModel(modelId, @"E:\My_Code\Demo\wpf.-prism\src\rect.shm");

            });

        }

        /// <summary>
        /// 绘制圆形
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_6(object sender, RoutedEventArgs e)
        {

        }
    }
}